from fastapi import APIRouter, UploadFile, File, HTTPException
from typing import List
from ..services.upload import UploadService
from ..models.request import UploadResponse
from ..utils.logger import setup_logger
import os

router = APIRouter()
logger = setup_logger('upload_api')

@router.post("/upload", response_model=UploadResponse)
async def upload_file(file: UploadFile = File(...)):
    """上传文件接口"""
    try:
        # 读取文件内容
        file_content = await file.read()
        
        # 处理文件上传
        pdf_files = UploadService.handle_upload(file_content, file.filename)
        
        # 返回结果
        return UploadResponse(
            message="文件上传成功",
            files=[os.path.basename(f) for f in pdf_files]
        )
        
    except Exception as e:
        logger.error(f"文件上传失败: {str(e)}")
        raise HTTPException(status_code=500, detail=str(e)) 